package com.psp.web.controller.springmvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;
import com.psp.web.controller.res.*;
import com.psp.web.controller.res.bean.*;
import com.psp.web.controller.springmvc.req.*;

/**
 * 用户-账户相关接口
 **/
@Controller
@RequestMapping(value = "/psp/user", produces = "application/json")
public class SUserController {
	@Autowired
	com.psp.web.controller.UserController userController;

	/**
	 * 用户注册
	 **/
	@RequestMapping("/v1/register")
	@ResponseBody
	public ObjectResult<RUserBean> register(@Validated RegisterParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RUserBean> res = new ObjectResult<RUserBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return userController.register(param, request, response);
	}

	/**
	 * 发送验证码
	 **/
	@RequestMapping("/v1/sendVCode")
	@ResponseBody
	public BaseResult sendVCode(@Validated SendVCodeParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return userController.sendVCode(param, request, response);
	}

	/**
	 * 激活邮箱
	 **/
	@RequestMapping("/v1/activateEmail")
	@ResponseBody
	public BaseResult activateEmail(@Validated AtivateEmailParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return userController.activateEmail(param, request, response);
	}

	/**
	 * 登录
	 **/
	@RequestMapping("/v1/login")
	@ResponseBody
	public ObjectResult<RUserBean> login(@Validated LoginParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RUserBean> res = new ObjectResult<RUserBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return userController.login(param, request, response);
	}

	/**
	 * 检查注册用户名是否合法
	 **/
	@RequestMapping("/v1/checkRegUser")
	@ResponseBody
	public BaseResult checkRegUser(@Validated CheckRegUserParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return userController.checkRegUser(param, request, response);
	}

	/**
	 * 检查登录用户名是否合法
	 **/
	@RequestMapping("/v1/checkLoginUser")
	@ResponseBody
	public BaseResult checkLoginUser(@Validated CheckLoginUserParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return userController.checkLoginUser(param, request, response);
	}

	/**
	 * 发送激活邮箱邮件
	 **/
	@RequestMapping("/v1/sendActivateEmail")
	@ResponseBody
	public BaseResult sendActivateEmail(@Validated SendAtivateEmailParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return userController.sendActivateEmail(param, request, response);
	}

	/**
	 * 找回密码验证手机
	 **/
	@RequestMapping("/v1/sendFindPwdCode")
	@ResponseBody
	public ObjectResult<RUserBean> sendFindPwdCode(@Validated SendFindPwdCodeParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RUserBean> res = new ObjectResult<RUserBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return userController.sendFindPwdCode(param, request, response);
	}

	/**
	 * 确认找回密码验证码
	 **/
	@RequestMapping("/v1/confirmFindPwdCode")
	@ResponseBody
	public ObjectResult<RUserBean> confirmFindPwdCode(@Validated ConfirmFindPwdCodeParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RUserBean> res = new ObjectResult<RUserBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return userController.confirmFindPwdCode(param, request, response);
	}

	/**
	 * 重设密码
	 **/
	@RequestMapping("/v1/resetPwd")
	@ResponseBody
	public BaseResult resetPwd(@Validated ResetPwdParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return userController.resetPwd(param, request, response);
	}

	/**
	 * 获取用户信息
	 **/
	@RequestMapping("/v1/getUser")
	@ResponseBody
	public ObjectResult<RUserBean> getUser(@Validated GetUserParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RUserBean> res = new ObjectResult<RUserBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return userController.getUser(param, request, response);
	}

	/**
	 * 修改用户信息
	 **/
	@RequestMapping("/v1/updateUser")
	@ResponseBody
	public ObjectResult<RUserBean> updateUser(@Validated UpdateUserParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RUserBean> res = new ObjectResult<RUserBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return userController.updateUser(param, request, response);
	}

	/**
	 * 验证手机号是否正确
	 **/
	@RequestMapping("/v1/checkUserPhone")
	@ResponseBody
	public BaseResult checkUserPhone(@Validated CheckUserPhoneParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return userController.checkUserPhone(param, request, response);
	}

	/**
	 * 更新手机号
	 **/
	@RequestMapping("/v1/updatePhone")
	@ResponseBody
	public ObjectResult<RUserBean> updatePhone(@Validated UpdatePhoneParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RUserBean> res = new ObjectResult<RUserBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return userController.updatePhone(param, request, response);
	}

	/**
	 * 重置邮箱,发送邮箱验证码
	 **/
	@RequestMapping("/v1/sendEmailVcode")
	@ResponseBody
	public BaseResult sendEmailVcode(@Validated SendEmailVcodeParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return userController.sendEmailVcode(param, request, response);
	}

	/**
	 * 验证手机号是否正确
	 **/
	@RequestMapping("/v1/checkUserEmail")
	@ResponseBody
	public BaseResult checkUserEmail(@Validated CheckUserEmailParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return userController.checkUserEmail(param, request, response);
	}

	/**
	 * 更新邮箱
	 **/
	@RequestMapping("/v1/updateEmail")
	@ResponseBody
	public ObjectResult<RUserBean> updateEmail(@Validated UpdateEmailParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RUserBean> res = new ObjectResult<RUserBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return userController.updateEmail(param, request, response);
	}

	/**
	 * 重置密码
	 **/
	@RequestMapping("/v1/resetPassword")
	@ResponseBody
	public BaseResult resetPassword(@Validated ResetPasswordParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return userController.resetPassword(param, request, response);
	}
}
